package com.ship.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ship.commens.Result;
import com.ship.dto.UserDto;
import com.ship.dto.UserSearchDto;
import com.ship.mapper.DeptMapper;
import com.ship.model.User;
import com.ship.service.UserService;
import com.ship.mapper.UserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author Lucky Boy
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-07-01 15:59:15
*/
@Service
@Transactional
public class UserServiceImpl implements UserService{


    @Resource
    private UserMapper userMapper;

    @Resource
    private DeptMapper deptMapper;

    @Override
    public Result findUsersByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        List<User> userList = userMapper.selectUsersByPage();//查所有
        PageInfo<User> pageInfo = new PageInfo<>(userList);//分页处理
        Result result = new Result(200, "访问成功");
        result.setData(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());

        return result;
    }

    @Override
    public Result saveUser(UserDto userDto) {
        //判断部门是否存在
        int rows = deptMapper.selectDeptExists(userDto.getDeptId());
        if (rows < 1){
            //部门不存在
            return new Result(-1, "部门不存在");
        }

        //判断职员是否已存在
        rows = userMapper.selectUserExists(userDto);
        if (rows > 0){
            //职员存在
            return new Result(-1, "职员已存在");
        }

        //添加人员信息
        rows = userMapper.insertUser(userDto);
        if (rows < 1){
            //添加失败
            return new Result(-1, "添加失败");
        }

        return Result.SUCCESS;
    }

    @Override
    public Result removeOneUser(Long id) {
        int rows = userMapper.deleteById(id);
        if(id < 1){
            return new Result(-1, "删除失败");
        }

        return Result.SUCCESS;
    }

    @Override
    public Result removeManyUser(String id) {
        if (id == null || !(id + ",").matches("([1-9][0-9]*,)+")){
            return Result.DATA_FORMAT_ERROR;
        }
        int rows = userMapper.deleteInIds(id);
        if(rows < 1){
            return new Result(-1, "删除失败");
        }
        return Result.SUCCESS;
    }

    @Override
    public Result modifyUser(UserDto userDto) {
        int rows = deptMapper.selectDeptExists(userDto.getDeptId());
        if (rows < 1){
            return new Result(-1, "部门不存在");
        }

        rows = userMapper.selectUserById(userDto.getId());
        if (rows < 1){
            return new Result(-1, "员工不存在");
        }

        rows = userMapper.updateUser(userDto);
        if(rows < 1){
            return new Result(-1, "修改失败");
        }
        return Result.SUCCESS;
    }

    @Override
    public Result findUsersBySearch(UserSearchDto userSearchDto) {
        PageHelper.startPage(userSearchDto.getPageNum(), userSearchDto.getPageSize());

        List<User> userList = userMapper.selectUsersBySearch(userSearchDto);

        PageInfo<User> pageInfo = new PageInfo<>(userList);

        Result result = Result.SUCCESS;
        result.setTotal(pageInfo.getTotal());
        result.setData(pageInfo.getList());
        return result;
    }
}
